package org.adream.account.rest.api;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

import org.adream.account.entity.GradeEntity;
import org.adream.account.model.ResultModel;
import org.adream.account.rest.api.model.GradeForm;
import org.adream.account.service.ADreamServiceException;
import org.adream.account.service.GradeService;
import org.adream.account.util.Utils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.PageInfo;

@RequestMapping("grade")
@Controller
public class GradeApiController {
	
	
	@Autowired
	private GradeService gradeService;
	
	private final static Logger logger = LoggerFactory.getLogger(GradeApiController.class);
	
	/**
	 * 增
	 * 
	 * @param gradeEntity
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "", produces = "application/json;charset=UTF-8", method = RequestMethod.POST)
	public ResultModel<String> addGrade(@RequestBody GradeEntity grade) {
		if(grade == null || StringUtils.isEmpty(grade.getGname())) {
			return new ResultModel<String>(ResultModel.ERR_PARAM, "新增失败,请求参数有误", null);
		}
		return gradeService.addGrade(grade);
	}
	
	/**
	 * 删除
	 * @param gid  grade id
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/{gid}", produces = "application/json;charset=UTF-8", method = RequestMethod.DELETE)
	public ResultModel<String> deleteGrade(@PathVariable("gid") Integer gid) {
		if(gid == null) {
			return new ResultModel<String>(ResultModel.ERR_PARAM, "删除失败,请求参数有误", null);
		}
		try {
			return gradeService.deleteGrade(gid);
		} catch (ADreamServiceException e) {
			logger.warn("删除职称/级别失败,{}", e.getMessage());
			return new ResultModel<String>(ResultModel.ERR_SYS, e.getMessage(), null);
		}
	}
	
	/**
	 * 改
	 * @param gradeEntity
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/{gid}", produces = "application/json;charset=UTF-8", method = RequestMethod.PUT)
	public ResultModel<String> updateGrade(@PathVariable("gid") Integer gid,@RequestBody GradeEntity gradeEntity) {
		if(gid == null || gradeEntity == null || StringUtils.isEmpty(gradeEntity.getGname())) {
			return new ResultModel<String>(ResultModel.ERR_PARAM, "更新失败,请求参数有误", null);
		}
		return gradeService.updateGrade(gid, gradeEntity);
	}
	
	/**
	 * 根据gid查询grade
	 * @param gid grade id
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/{gid}", produces = "application/json;charset=UTF-8",method = RequestMethod.GET)
	public ResultModel<GradeEntity> queryGradeByGid(@PathVariable("gid") Integer gid) {
		if(gid == null) {
			return new ResultModel<GradeEntity>(ResultModel.ERR_PARAM, "查询失败,请求参数有误", null);
		}
		GradeEntity grade = gradeService.queryGradeByGid(gid);
		if(grade == null) {
			logger.warn("职称/级别数据不存在,gid:{}", gid);
			return new ResultModel<GradeEntity>(ResultModel.NOT_FOUND_OBJECT, "查询失败,数据不存在", null);
		}
		return new ResultModel<GradeEntity>(ResultModel.SUCCESS, ResultModel.SUCCESS_MSG, grade);
	}
	
	/**
	 * 根据删除标志查询grade
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/dr/{dr}", produces = "application/json;charset=UTF-8",method = RequestMethod.GET)
	public ResultModel<List<GradeEntity>> queryGradesByDr(@PathVariable("dr") Integer dr) {
		if(dr == null) {
			return new ResultModel<List<GradeEntity>>(ResultModel.ERR_PARAM, "查询失败,请求参数有误", null);
		}
		List<GradeEntity> grades = gradeService.queryGradesByDr(dr);
		if(Utils.isEmptyCollection(grades)) {
			return new ResultModel<List<GradeEntity>>(ResultModel.NOT_FOUND_OBJECT, "查询失败,数据不存在", null);
		}
		return new ResultModel<List<GradeEntity>>(ResultModel.SUCCESS, ResultModel.SUCCESS_MSG, grades);
	}
	
	/**
	 * 根据删除标志,父级id查询grade
	 * @param dr 删除标志
	 * @param pid 父级id
	 * @return
	 */
	@RequestMapping(value = "/pid/{pid}/dr/{dr}", produces = "application/json;charset=UTF-8", method = RequestMethod.GET)
	@ResponseBody
	public ResultModel<List<GradeEntity>> queryGradesByPidAndDr(@PathVariable("pid") Integer pid,@PathVariable("dr") Integer dr) {
		if(pid == null || dr == null) {
			return new ResultModel<List<GradeEntity>>(ResultModel.ERR_PARAM,"查询失败,请求参数有误",null);
		}
		List<GradeEntity> grades = gradeService.queryGradesByPidAndDr(pid, dr);
		if(Utils.isEmptyCollection(grades)) {
			return new ResultModel<List<GradeEntity>>(ResultModel.NOT_FOUND_OBJECT, "查询失败,数据不存在", null);
		}
		return new ResultModel<List<GradeEntity>>(ResultModel.SUCCESS, ResultModel.SUCCESS_MSG, grades);
	}
	
	/**
	 * 根据dr,gname,分页查询grade
	 * @param form  请求表单
	 * @return
	 */
	@RequestMapping(value = "/queryGradesBySomeFields", produces = "application/json;charset=UTF-8", method = RequestMethod.POST)
	@ResponseBody
	public ResultModel<PageInfo<GradeEntity>> queryGradesByDrAndPidAndLikeGname(@RequestBody GradeForm form) {
		if(form == null || form.getDr() == null) {
			return new ResultModel<PageInfo<GradeEntity>>(ResultModel.ERR_PARAM, "请求参数为空", null);
		} 
		PageInfo<GradeEntity> page = gradeService.queryGradesBySomeFields(form.getDr(), form.getPid(), StringUtils.trimWhitespace(form.getGname()),form.getPageNo(),form.getPageSize());
		if(page == null || Utils.isEmptyCollection(page.getList())) {
			return new ResultModel<PageInfo<GradeEntity>>(ResultModel.NOT_FOUND_OBJECT, "查询失败,数据不存在", null);
		}
		return new ResultModel<PageInfo<GradeEntity>>(ResultModel.SUCCESS, "成功获取grade", page);
	}
	
	/**
	 * 检查 gname是否已存在
	 * @param gname
	 * @return
	 */
	@RequestMapping(value = "/checkGname")
	public void checkGname(String gname,HttpServletResponse response) {
		boolean result = gradeService.isValidGname(gname);
		PrintWriter out = null;
		try {
			out = response.getWriter();
			out.write(String.valueOf(result));
		} catch (IOException e) {
			logger.info("检查gname出错:)");
		} finally {
			if(out != null) {
				out.close();
			}
		}
	}
}